package view;

import db.AIPlayer;
import db.Player;
import mechanism.GoBangBoard;
import result.Result;
import util.Client;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashMap;
import java.util.Objects;

/**
 * @author 化尘成影
 * @Description
 * @date 2022/6/27 13:34
 */
public class Room extends JFrame{
    private String username;

    public Room(String username) {
        super();
        Client.exit = true;
        this.username = username;
        initFrame();
    }

    public void initFrame() {
        setTitle("五子棋[房间]");
        setSize(500, 500);
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        MyJPanel myJPanel = new MyJPanel(this);
        this.setLocationRelativeTo(null);
        setContentPane(myJPanel);
    }

    private class MyJPanel extends JPanel implements MouseListener {
        Font font = new Font(Font.SERIF, Font.PLAIN, 30);
        Room roomForm;
        JLabel l1 = new JLabel("房间界面");
        JButton create = new JButton("创建对局");
        JButton join = new JButton("加入对局");
        JButton exit = new JButton("登出");
        JButton table = new JButton("积分榜");
        JButton playWithAi = new JButton("人机对战");
        JTextField createId = new JTextField();
        JTextField joinId = new JTextField();

        ImageIcon icon;
        Image img;

        public MyJPanel(Room roomForm) {
            super();
            setLayout(null);
            this.roomForm = roomForm;
            this.addMouseListener(this);

            //设置组件信息
            l1.setBounds(20,50,450,50);
            l1.setFont(font);
            create.setBounds(100, 110, 100, 50);
            createId.setBounds(200, 125, 200, 25);
            join.setBounds(100, 180, 100, 50);
            joinId.setBounds(200, 200, 200, 25);
            exit.setBounds(100, 320, 100, 50);
            table.setBounds(300, 320, 100, 50);
            playWithAi.setBounds(200,320,100,50);

            //添加到面板去
            add(l1);
            add(create);
            add(createId);
            add(join);
            add(joinId);
            add(exit);
            add(table);
            add(playWithAi);

            //获取背景图
            icon = new ImageIcon(getClass().getResource("/image/a.png"));
            img = icon.getImage();

            //创建房间
            create.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (createId.getText().equals("")) {
                        JOptionPane.showMessageDialog(roomForm, "房间id不能为空");
                    } else {
                        HashMap<String, String> map = new HashMap<>();
                        map.put("createId",createId.getText());
                        map.put("fromUser",username);
                        Result result = Client.request("create", map,true);
                        if (result.getCodeMsg().getCode() == 200) {
                            JOptionPane.showMessageDialog(roomForm, "创建成功");

                            GoBangBoard goBangBoard = new GoBangBoard();
                            goBangBoard.owner = new Player(username, "黑棋玩家", true, true, "X", 3);
                            Client.goBangBoard = goBangBoard;
                            Board board = new Board(username,null,createId.getText());
                            Client.board = board;
                            roomForm.dispose();
                            Client.clientResponse();
                            board.setVisible(true);
                        } else if(Objects.equals(result.getCodeMsg().getCode(),404)){
                            JOptionPane.showMessageDialog(roomForm, result.getCodeMsg().getMsg());
                            roomForm.dispose();
                            new Login().setVisible(true);
                        } else {
                            JOptionPane.showMessageDialog(roomForm,result.getCodeMsg().getMsg());
                        }
                    }
                    System.out.println("创建房间");
                }
            });

            //加入房间
            join.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (joinId.getText().equals("")) {
                        JOptionPane.showMessageDialog(roomForm, "房间id不能为空");
                    } else {
                        HashMap<String, String> map = new HashMap<>();
                        map.put("joinId",joinId.getText());
                        map.put("fromUser",username);
                        Result result = Client.request("join", map,true);
                        if (result.getCodeMsg().getCode() == 200) {
                            JOptionPane.showMessageDialog(roomForm, "加入成功");

                            GoBangBoard goBangBoard = new GoBangBoard();
                            goBangBoard.owner = new Player(username, "白棋玩家", false, false, "O", 3);
                            goBangBoard.rival = new Player(result.getMap().get("rival"), "黑棋玩家", true, true, "X", 3);
                            Client.goBangBoard = goBangBoard;

                            Board board = new Board(username, result.getMap().get("rival"), joinId.getText());
                            Client.board = board;

                            roomForm.dispose();
                            Client.clientResponse();
                            Client.isContinue = true;
//                            Client.rivalStillLive();
                            board.setVisible(true);
                        } else if(Objects.equals(result.getCodeMsg().getCode(),404)){
                            JOptionPane.showMessageDialog(roomForm, result.getCodeMsg().getMsg());
                            roomForm.dispose();
                            new Login().setVisible(true);
                        } else {
                            JOptionPane.showMessageDialog(roomForm,result.getCodeMsg().getMsg());
                        }
                    }
                    System.out.println("加入房间");
                }
            });

            //登出
            exit.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    int result = JOptionPane.showConfirmDialog(null, "是否决定登出？", "判断框", JOptionPane.YES_NO_OPTION);
                    if(result == 0){
                        System.out.println("登出");
                        HashMap<String, String> map = new HashMap<>();
                        map.put("fromUser",username);
                        Client.request("exit", map, false);
                        Login login = new Login();
                        roomForm.dispose();
                        login.setVisible(true);
                    } else {
                        System.out.println("放弃登出");
                    }
                }
            });

            //积分榜
            table.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Table table = new Table();
                    table.show();
                }
            });

            //人机对局
            playWithAi.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String[] role = new String[]{"黑棋玩家","白棋玩家"};
                    int result = JOptionPane.showOptionDialog(null,"请选择黑棋玩家或白棋玩家","选择框",JOptionPane.DEFAULT_OPTION,
                            JOptionPane.QUESTION_MESSAGE,null,role,"黑棋玩家");
                    System.out.println("开启人机对局，用户为" + role[result]);

                    GoBangBoard goBangBoard = new GoBangBoard();
                    boolean isBlack = result == 0;
                    //构建玩家
                    goBangBoard.owner = new Player(username,role[result],isBlack,isBlack,isBlack ? "X" : "O",3);
                    goBangBoard.rival = new AIPlayer(!isBlack ? "黑棋玩家" : "白棋玩家",!isBlack,!isBlack,!isBlack ? "X":"O");
                    goBangBoard.isPlayWithAi = true;
                    goBangBoard.isYour = goBangBoard.owner.getYour();
                    Client.goBangBoard = goBangBoard;

                    Board board = new Board(username, goBangBoard.rival.getUsername(), "人机对战局");
                    if(result == 1){
                        goBangBoard.newBoard();
                        goBangBoard.addChess(8,8);
                    }
//                    goBangBoard.aiAdd();
                    Client.board = board;
                    roomForm.dispose();
                    board.setVisible(true);
                }
            });
        }

        //添加背景图
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            g.drawImage(img,0,0,this.getWidth(),this.getHeight(),this);
        }

        @Override
        public void mouseClicked(MouseEvent e) {

        }

        @Override
        public void mousePressed(MouseEvent e) {

        }

        @Override
        public void mouseReleased(MouseEvent e) {

        }

        @Override
        public void mouseEntered(MouseEvent e) {

        }

        @Override
        public void mouseExited(MouseEvent e) {

        }
    }
}
